//
//  WGWheathCollectionViewCell.m
//  QuickCure1
//
//  Created by Apple on 16/10/31.
//  Copyright © 2016年 maoge. All rights reserved.
/*
 
 ///  天气照片
 @property (nonatomic, copy) NSString *dayPictureUrl;
 
 /// 天气 `阴天`
 @property (nonatomic, copy) NSString *weather;
 
 ///  温度
 @property (nonatomic, copy) NSString *temperature;
 
 ///  日期
 @property (nonatomic, copy) NSString *date;
 
 
 */

#import "WGWheathCollectionViewCell.h"
#import <UIImageView+WebCache.h>
#define kColor 0x15DECA

@interface WGWheathCollectionViewCell ()
@property (weak, nonatomic) IBOutlet UIImageView *iv_dayImage;

@property (weak, nonatomic) IBOutlet UILabel *lb_wheather;



@property (weak, nonatomic) IBOutlet UILabel *lb_temperature;

@property (weak, nonatomic) IBOutlet UILabel *lb_date;
@property (weak, nonatomic) IBOutlet UILabel *lb_realTem;

@end

@implementation WGWheathCollectionViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

+(instancetype)weatherCell{
    
    return [[[NSBundle mainBundle]loadNibNamed:@"WGWheathCollectionViewCell" owner:nil options:nil]lastObject];
}

-(void)setModel:(Weather *)model{
    /**
     
     date = "周五 11月04日 (实时：17℃)";
     dayPictureUrl = "http://api.map.baidu.com/images/weather/day/qing.png";
     temperature = "20 ~ 14℃";
     weather = "晴转多云"
     
     */
    
    _model = model;
    
    //获取model前两个字符串
    
    NSString *week = [model.date substringWithRange:NSMakeRange(0, 2)];
#warning 解决获取实时天气的bug
    //截取字符串
    NSString *subStr = [model.date substringFromIndex:12];
    //扫描字符串
    if (subStr) {
        NSScanner *scanner = [NSScanner scannerWithString:subStr];
        [scanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:nil];
        int number;
        [scanner scanInt:&number];
        NSString *realTemp=[NSString stringWithFormat:@"%d",number];
        self.lb_realTem.text = [NSString stringWithFormat:@"%@°",realTemp];
    }
    self.lb_realTem.font = [UIFont fontWithName:@"AppleGothic" size:50 ];
    
    

    NSDate *date = [NSDate date];
    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];
    fmt.dateFormat = @"MM / dd";
    NSString *dataStr = [fmt stringFromDate:date];
    
    self.lb_date.text =[NSString stringWithFormat:@"%@   %@",dataStr,week ] ;
    self.lb_temperature.text = [NSString stringWithFormat:@"%@",model.temperature];
    self.lb_wheather.text = model.weather;
    
    //设置天气图片
    [self setWeatherImageWithModel:model];
    
    //设置天气动画
    [self setWheatherAnimation];
    
    //设置城市按钮
    [self setCityTitleAttributes];
    
}

#pragma
#pragma mark - 设置天气图片的动画
-(void)setWheatherAnimation{
    
    //1.创建动画
    CABasicAnimation *basicAm = [CABasicAnimation animationWithKeyPath:@"position"];
    
    basicAm.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
    
    basicAm.repeatCount = 1;
    
    basicAm.duration = 1;
    
    //禁用回弹
    basicAm.fillMode = kCAFillModeForwards;
    basicAm.removedOnCompletion = NO;
    
    //3.添加动画
    [self.iv_dayImage.layer  addAnimation:basicAm forKey:@"basic"];
    
}


#pragma
#pragma mark - 设置天气图片
-(void)setWeatherImageWithModel:(Weather*)model{
    
    
    if ([model.weather isEqualToString:@"晴转多云"]) {
        
        self.iv_dayImage.image = [UIImage imageNamed:@"sun"];
    }else if([model.weather isEqualToString:@"多云"]){
        
        self.iv_dayImage.image = [UIImage imageNamed:@"duoyun"];
    }else if([model.weather isEqualToString:@"晴"]){
        
        self.iv_dayImage.image = [UIImage imageNamed:@"sun"];
        
    }else if([model.weather isEqualToString:@"阵雨转多云"]){
        
        self.iv_dayImage.image = [UIImage imageNamed:@"yin"];
    }  else if([model.weather isEqualToString:@"霾"]){
        
        self.iv_dayImage.image = [UIImage imageNamed:@"yin"];
    }else{
        
        //        NSURL *dayUrl = [NSURL URLWithString:model.dayPictureUrl];
        //        [self.iv_dayImage sd_setImageWithURL:dayUrl];
        self.iv_dayImage.image = [UIImage imageNamed:@"yin"];
        
    }
    
    
}


#pragma
#pragma mark - 设置button按钮的文字下划线

-(void)setCityTitleAttributes{
    
    NSString *city = self.btn_city.currentTitle;
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:city];
    NSRange strRange = {0,[str length]};
    
    [str setAttributes:@{NSForegroundColorAttributeName :[UIColor redColor],NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]} range:strRange];
    
    [self.btn_city setAttributedTitle:str forState:UIControlStateNormal];
    self.btn_city.titleLabel.font = [UIFont fontWithName:@"AppleGothic" size:22];
    
}
#pragma
#pragma mark - 点击城市button
- (IBAction)clickCitybutton:(UIButton *)sender {
    
    if ([self.delegate respondsToSelector:@selector(clickCityButtonWithWheathCollectionViewCell:)]) {
        
        [self.delegate clickCityButtonWithWheathCollectionViewCell:self];
    }
    
    
    if ([self.delegate respondsToSelector:@selector(clickCityButtonWithWheathCollectionViewCell:withCityButton:)]) {
        [self.delegate clickCityButtonWithWheathCollectionViewCell:self withCityButton:sender];
        
    }
    
}

@end
